﻿using GomuLibrary.IO.DiscImage;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using zyconfig;

namespace zymain
{
    public partial class FrmMakeISO : Form
    {
        public FrmMakeISO()
        {
            InitializeComponent();
        }
        private void FrmMakeISO_Load(object sender, EventArgs e)
        {
            this.iso.Terminate += new EventHandler(this.iso_Terminate);
            this.iso.Aborted += new EventHandler<Iso9660CreatorEventArgs>(this.iso_Aborted);
            this.iso.Progression += new EventHandler<Iso9660CreatorEventArgs>(this.iso_Progression);
            DriveInfo[] drives = DriveInfo.GetDrives();
            for (int i = 0; i < drives.Length; i++)
            {
                if (drives[i].DriveType == DriveType.CDRom)
                {
                    this.cmbDrive.Items.Add(drives[i].Name);
                }
            }
            if (this.cmbDrive.Items.Count > 0)
            {
                this.cmbDrive.SelectedIndex = 0;
            }
            if (Directory.Exists(DbConfig.WorkDir))
            {
                this.tbxSaveas.Text = DbConfig.WorkDir + DateTime.Now.ToString("yyyyMMddhhmmss") + ".iso";
            }
            else
            {
                this.tbxSaveas.Text = Application.StartupPath + @"\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".iso";
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.label5.Visible = true;
            this.button2.Enabled = true;
            this.button1.Enabled = false;
            this.progressBarX1.Maximum = Convert.ToInt32((long)(new DriveInfo(this.cmbDrive.SelectedItem.ToString()).TotalSize / 10L));
            this.iso.CreateImage(this.cmbDrive.SelectedItem.ToString(), this.tbxSaveas.Text, true);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认要取消本次光盘镜像的制作吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                this.iso.Abort();
                this.button2.Enabled = false;
                this.button1.Enabled = true;
                this.label5.Visible = false;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            base.Close();
        }
        private void lnkSaveas_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            using (SaveFileDialog dialog = new SaveFileDialog())
            {
                dialog.CheckPathExists = true;
                dialog.Filter = "Iso File (.iso)|*.iso";
                dialog.DefaultExt = ".iso";
                dialog.AddExtension = true;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    this.tbxSaveas.Text = dialog.FileName;
                }
            }
        }
        private void DisplayProgress(Iso9660CreatorEventArgs arg)
        {
            this.progressBarX1.Value = Convert.ToInt32((long)(arg.BytesWritted / 10L));
        }

        private void iso_Aborted(object sender, Iso9660CreatorEventArgs e)
        {
        }

        private void iso_Progression(object sender, Iso9660CreatorEventArgs e)
        {
            if (base.InvokeRequired)
            {
                ProgressionEventHandler method = new ProgressionEventHandler(this.DisplayProgress);
                base.Invoke(method, new object[] { e });
            }
        }

        private void iso_Terminate(object sender, EventArgs e)
        {
            if (MessageBox.Show("处理完毕！是否要上传本ISO文件", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                base.DialogResult = DialogResult.OK;
                base.Close();
            }
        }

        private delegate void ProgressionEventHandler(Iso9660CreatorEventArgs e);

        public string FilePath
        {
            get
            {
                return this.tbxSaveas.Text;
            }
            set
            {
                this.tbxSaveas.Text = value;
            }
        }
    }
}
